package com.iexbuy.ihg.pay;

import com.baidu.navisdk.util.common.StringUtils;
import com.iexbuy.ihg.common.CommUtil;
import com.iexbuy.ihg.net.ServiceHelper;
import com.iexbuy.ihg.pay.aliy.AliyPay;
import com.iexbuy.ihg.wxapi.WeixinPay;
import com.iexbuy.ihg.ui.activity.BaseActivity;
import com.iexbuy.ihg.utils.JsonUtils;
import com.iexbuy.ihg.utils.ToastUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;

import java.util.HashMap;

/**
 * Created by chenguiban on 15/11/1.
 */
public class PayUtil {

    /**
     * 现金余额支付
     * @param mBaseActivity
     * @param tradeNo
     * @param listener
     */
    public static void requestCashBalancePay(final BaseActivity mBaseActivity, final String tradeNo, final OnPayCompleteListener listener) {
        HashMap<String, String> paramMap = new HashMap<String, String>();
        paramMap.put("orderId", tradeNo);
        ServiceHelper.send(mBaseActivity, ServiceHelper.PAY_BY_CASH, paramMap,
                new RequestCallBack<String>() {
                    @Override
                    public void onStart() {
                        mBaseActivity.showDialog("提交中...");
                    }

                    @Override
                    public void onCancelled() {
                        mBaseActivity.closeDialog();
                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        mBaseActivity.closeDialog();
                        if (listener != null ) {
                            listener.onPayComplete(responseInfo.result);
                        }
                    }

                    @Override
                    public void onFailure(HttpException error, String msg) {
                        mBaseActivity.closeDialog();
                        CommUtil.showAlert(mBaseActivity, "请求出错", msg, null);
                    }
                });
    }

    /**
     * 支付宝支付
     */
    public static void requestAlipay(BaseActivity mBaseActivity,  String productName,  String description,  String tradeNo,  String price,  OnPayCompleteListener listener) {
        AliyPay alipy = new AliyPay();
        alipy.setmContext(mBaseActivity);
        alipy.setListener(listener);
        alipy.pay(productName, description, price, tradeNo, ServiceHelper.ALIYPAY_NOTIFY);
    }

    /**
     * 微信支付，先从后台服务器向微信服务器请求获取微信预支付信息，再从本地发起微信支付
     */
    public static void requestWeixinpay(final BaseActivity mBaseActivity,
                                        String tradeNo,
                                        String productName,
                                        String productDescription,
                                        String amount,
                                        String notifyURL) {
        //从服务器获取微信支付信息
        HashMap<String, String> paramMap = new HashMap<String, String>();
        paramMap.put("out_trade_no", tradeNo);
        if ("xiaofei".equalsIgnoreCase(productDescription)) {
            paramMap.put("type", "");
        } else if (productDescription.contains("金币")) {
            paramMap.put("type", "jbcz");
        }
        ServiceHelper.send(mBaseActivity, ServiceHelper.WX_PREPAY, paramMap,
                new RequestCallBack<String>() {
                    @Override
                    public void onStart() {
                        mBaseActivity.showDialog("正在向微信请求支付信息...");
                    }

                    @Override
                    public void onCancelled() {
                        mBaseActivity.closeDialog();
                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        mBaseActivity.closeDialog();
                        String prePayInfo = JsonUtils.getJsonSubStringByKey(responseInfo.result, "body");
                        if (StringUtils.isNotEmpty(prePayInfo)) {
                            WeixinPay.sendPayReq(mBaseActivity,
                                    JsonUtils.getJsonSubStringByKey(prePayInfo, "appid"),
                                    JsonUtils.getJsonSubStringByKey(prePayInfo, "partnerid"),
                                    JsonUtils.getJsonSubStringByKey(prePayInfo, "prepayid"),
                                    JsonUtils.getJsonSubStringByKey(prePayInfo, "noncestr"),
                                    JsonUtils.getJsonSubStringByKey(prePayInfo, "timestamp"),
                                    JsonUtils.getJsonSubStringByKey(prePayInfo, "package"),
                                    JsonUtils.getJsonSubStringByKey(prePayInfo, "sign"));
                        } else {
                            ToastUtils.toast(mBaseActivity, "获取微信支付信息失败，请稍后再试。");
                        }
                    }

                    @Override
                    public void onFailure(HttpException error, String msg) {
                        mBaseActivity.closeDialog();
                        ToastUtils.toast(mBaseActivity, msg);
                    }
                });
    }

    public interface OnPayCompleteListener{
        public void onPayComplete(String resultStr);
    }

}
